local jisi = fk.CreateSkill {
  name = "emo__jisi",
}

jisi:addEffect(fk.Damaged, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(jisi.name) then
      return #player:getTableMark("@emo__jisi_Record") > 0
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local mark = player:getMark("@emo__jisi_Record")
    room:setPlayerMark(player, "@emo__jisi_Record", 0)
    local cardsMap = {}
    for _, id in ipairs(room.draw_pile) do
      local suit = Fk:getCardById(id):getSuitString(true)
      if table.contains(mark, suit) then
        cardsMap[suit] = cardsMap[suit] or {}
        table.insert(cardsMap[suit], id)
      end
    end
    local cards = {}
    for _, ids in pairs(cardsMap) do
      table.insert(cards, table.random(ids))
    end
    if #cards > 0 then
      room:obtainCard(player, cards, false, fk.ReasonPrey, player.id, self.name)
    end
  end,
})

jisi:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(jisi.name) and #player:getTableMark("@emo__jisi_Record") < 4
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local mark = player:getTableMark("@emo__jisi_Record")
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile and move.moveReason ~= fk.ReasonUse and move.moveReason ~= fk.ReasonResponse then
        for _, info in ipairs(move.moveInfo) do
          local card = Fk:getCardById(info.cardId)
          if card.suit ~= Card.NoSuit then
            table.insertIfNeed(mark, card:getSuitString(true))
          end
        end
      end
    end
    room:setPlayerMark(player, "@emo__jisi_Record", #mark > 0 and mark or 0)
  end,
})

jisi:addLoseEffect(function (self, player)
  player.room:setPlayerMark(player, "@emo__jisi_Record", 0)
end)

Fk:loadTranslationTable{
  ["emo__jisi"] = "集思",
  [":emo__jisi"] = "每当一张牌不因使用或打出而进入弃牌堆后，若此牌的花色未被“集思”记录，你记录此花色。每当你受到伤害后，你可以摸“集思”记录的花色各一张牌，然后清空记录。",
  ["@emo__jisi_Record"] = "集思",
}

return jisi
